<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

use think\Request;

class Record extends Backend {
    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->RecordModel = model( 'User.Record' );
        $this->UserModel = model( 'User.User' );
    }

    // 主页显示

    public function index() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        $userId = $this->request->param( 'id', '' );

        if ( !empty( $userId ) ) {
            $where[ 'userid' ] = $userId;
        }

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'content' ] = [ 'like', "%$keywords%" ];
        }

        // 获取数量
        $recordCount = $this->RecordModel->count();

        // 获取全部消息
        $recordList = $this->RecordModel->where( $where )->order( 'createtime', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        foreach ( $recordList as $key => $item ) {
            $userInfo = $this->UserModel->where( [ 'id' => $item[ 'userid' ] ] )->find();
            if ( $userInfo ) {
                $item[ 'userid' ] = $userInfo[ 'nickname' ];
            }

            switch( intval( $item[ 'state' ] ) ) {
                case 1:
                $item[ 'state' ] = '发布新帖';
                break;
                case 2:
                $item[ 'state' ] = '采纳评论';
                break;
                case 3:
                $item[ 'state' ] = '充值积分';
                break;
                case 4:
                $item[ 'state' ] = '每日签到';
                break;
                case 5:
                $item[ 'state' ] = '商品订单';
                break;
                case 6:
                $item[ 'state' ] = '预约订单';
                break;
                case -1:
                $item[ 'state' ] = '任何退款';
                break;
                default:
                break;
            }
        }
        // 获取分页显示
        $recordListstr = $recordList->render();

        $this->assign( [
            'recordCount' => $recordCount,
            'recordList' => $recordList,
            'recordListstr' => $recordListstr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    // 删除记录

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 软删除
            $result = $this->RecordModel->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->UserModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除记录成功';
            }

            return json( $success );
        }
    }
}
?>